package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.UacMerchantMapper;
import com.csun.cmny.provider.model.constant.EmpConstant;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.merchant.MerchantPersonageSelectDto;
import com.csun.cmny.provider.model.dto.merchant.MerchantQueryDto;
import com.csun.cmny.provider.model.dto.merchant.MerchantSaveDto;
import com.csun.cmny.provider.model.enums.UacRoleCodeEnum;
import com.csun.cmny.provider.model.enums.UacUserStatusEnum;
import com.csun.cmny.provider.model.exceptions.UacBizException;
import com.csun.cmny.provider.model.vo.merchant.MerchantVo;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.utils.Md5Util;
import com.csun.cmny.provider.utils.OnOffTimeUtil;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class UacMerchantServiceImpl extends BaseService<UacMerchant> implements UacMerchantService {

    @Resource
    private UacMerchantMapper merchantMapper;
    @Resource
    private UacUserService userService;
    @Resource
    private UacRoleService roleService;
    @Resource
    private UacRoleUserService roleUserService;
    @Resource
    private UacGroupUserService groupUserService;
    @Resource
    private UacSecondLvelMerchantBindService secondLvelMerchantBindService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;






    @Override
    public List<MerchantVo> selectMerchant(MerchantQueryDto merchantQueryDto) {
        return merchantMapper.selectMerchant(merchantQueryDto);
    }

    @Override
    public List<MerchantVo> listMerchantByIdList(List<Long> userList) {
        return merchantMapper.listMerchantByIdList(userList);
    }

    @Override
    public Integer checkMerchantOnline(Long merchantId) {

        UacMerchant uacMerchant = merchantMapper.selectByPrimaryKey(merchantId);
       /* if (uacMerchant.getIsManual()==0){
            if(PublicUtil.isNotEmpty(uacMerchant.getOnTime()) && PublicUtil.isNotEmpty(uacMerchant.getOffTime())){
                boolean business = OnOffTimeUtil.isBusiness(uacMerchant.getOnTime(), uacMerchant.getOffTime());
                if(business){
                    return uacMerchant.getIsBusiness();
                }
            }
        }else {
            return uacMerchant.getIsBusiness();
        }*/

        return uacMerchant.getIsBusiness();
    }

    @Override
    public int insertMerchant(MerchantSaveDto merchantSaveDto) {

        // 新增账号
        UacUser uacUser = new UacUser();
        uacUser.setLoginName(merchantSaveDto.getPrincipalPhone());
        uacUser.setLoginPwd(Md5Util.encrypt(EmpConstant.EMP_DEFAULT_PWD)
        );
        uacUser.setUserName(merchantSaveDto.getMerchantName());
        uacUser.setMobileNo(merchantSaveDto.getPrincipalPhone());
        uacUser.setStatus(UacUserStatusEnum.ENABLE.getKey());
        uacUser.setRoleCode(UacRoleCodeEnum.BUSINESS.getCode());

        userService.save(uacUser);

        //新增用户和角色的绑定关系

        //校验商家角色是否存在
        UacRole uacRole = new UacRole();
        uacRole.setRoleCode("business");
        UacRole role = roleService.selectOne(uacRole);
        if(PublicUtil.isEmpty(role)){
            throw new UacBizException(ErrorCodeEnum.DMC100618003);
        }

        UacRoleUser uacRoleUser = new UacRoleUser();
        uacRoleUser.setUserId(uacUser.getId());
        uacRoleUser.setRoleId(role.getId());
        roleUserService.save(uacRoleUser);

        //新增用户和机构的绑定关系
        UacGroupUser uacGroupUser=new UacGroupUser();
        uacGroupUser.setUserId(uacUser.getId());
        uacGroupUser.setGroupId(merchantSaveDto.getGroupId());
        groupUserService.save(uacGroupUser);


      /*  //新增员工
        UacEditEmpDto uacEditEmpDto = new UacEditEmpDto();
        uacEditEmpDto.setLoginName(merchantSaveDto.getPrincipalPhone());
       //uacEditEmpDto.setEmpDate(appraiserSaveDto.getEmpDate());
        uacEditEmpDto.setIdCard(merchantSaveDto.getPrincipalIDCard());
        uacEditEmpDto.setMobileNo(merchantSaveDto.getPrincipalPhone());
       //uacEditEmpDto.setDeptId(Long.parseLong(appraiserSaveDto.getAdministrative()));
        uacEditEmpDto.setRoleId(21L);
        empService.addUacEmpWithRole(uacEditEmpDto, getLoginAuthDto());


        UacUser uacUser = new UacUser();
        uacUser.setLoginName(merchantSaveDto.getPrincipalPhone());
        List<UacUser> users = userService.select(uacUser);*/


        /*新增商家*/
        UacMerchant uacMerchant = new UacMerchant();
        BeanUtils.copyProperties(merchantSaveDto,uacMerchant);

        uacMerchant.setOperator(merchantSaveDto.getUserName());
        uacMerchant.setGroupId(merchantSaveDto.getGroupId());
        uacMerchant.setUserId(merchantSaveDto.getUserId());
        uacMerchant.setUserIdBind(uacUser.getId());

        int save = save(uacMerchant);

        //将新增的商家信息写入redis
        redisTemplate.opsForValue().set(RedisKeyUtil.getUacMerchant(uacMerchant.getId().toString()),uacMerchant);


        //新增商家与二级服务类别的绑定关系
        for (Long secondLevelId : merchantSaveDto.getSecondLevelIds()) {

            UacSecondLvelMerchantBind uacSecondLvelMerchantBind = new UacSecondLvelMerchantBind();

            uacSecondLvelMerchantBind.setMerchantId(uacMerchant.getId());
            uacSecondLvelMerchantBind.setSecondLevelId(secondLevelId);

            secondLvelMerchantBindService.save(uacSecondLvelMerchantBind);


        }

        return  save;

    }

    @Override
    public int deleteMerchant(Long id) {

        //删除用户资料
        UacMerchant uacMerchant = selectByKey(id);
        userService.deleteByKey(uacMerchant.getUserIdBind());


        //删除商家信息
        int delete = deleteByKey(id);

        UacSecondLvelMerchantBind secondLvelMerchantBind = new UacSecondLvelMerchantBind();
        secondLvelMerchantBind.setMerchantId(id);



        //删除商家绑定信息
        secondLvelMerchantBindService.delete(secondLvelMerchantBind);


        return delete;
    }

    @Override
    public List<MerchantVo> selectPersonage(MerchantPersonageSelectDto merchantPersonageSelectDto) {
        return merchantMapper.selectPersonage(merchantPersonageSelectDto);
    }
}
